/**
 * This file is part of aion-engine <aion-engine.com>
 *
 *  aion-engine is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-engine is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser Public License
 *  along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */

package quest.altgard;

import com.aionemu.gameserver.model.gameobjects.Npc;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.questEngine.handlers.QuestHandler;
import com.aionemu.gameserver.questEngine.model.QuestEnv;
import com.aionemu.gameserver.questEngine.model.QuestState;
import com.aionemu.gameserver.questEngine.model.QuestStatus;
import com.aionemu.gameserver.services.QuestService;

/**
 * @author HellBoy
 */
public class _2230AFriendlyWager extends QuestHandler {

    private final static int questId = 2230;

    public _2230AFriendlyWager() {
        super(questId);
    }

    @Override
    public void register() {
        qe.setNpcQuestData(203621).addOnQuestStart(questId);
        qe.setNpcQuestData(203621).addOnTalkEvent(questId);
    }

    @Override
    public boolean onDialogEvent(QuestEnv env) {
		Player player = env.getPlayer();
        QuestState qs = env.getPlayer().getQuestStateList().getQuestState(questId);
		
		int targetId = 0;
		if(env.getVisibleObject() instanceof Npc)
			targetId = ((Npc) env.getVisibleObject()).getNpcId();
			
        if (qs == null || qs.getStatus() == QuestStatus.NONE) {
            if (targetId == 203621) {
                switch (env.getDialogId()) {
                    case 25:
                        return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 1011);
                    case 1007:
                        return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 4);
                    case 1002:
                        return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 1003);
                    case 1003:
                        return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 1004);
                    case 10000:
                        if (QuestService.startQuest(env, QuestStatus.START)) {
                            QuestService.questTimerStart(env, 1800);
                            return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 0);
                        } else
                            return false;
                }
            }
        }
        if (qs == null)
            return false;
        int var = qs.getQuestVarById(0);
        if (qs.getStatus() == QuestStatus.START) {
            if (targetId == 203621) {
                switch (env.getDialogId()) {
                    case 25:
                        if (var == 0)
                            return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 2375);
                    case 33:
                        if (var == 0) {
                            if (QuestService.collectItemCheck(env, true)) {
                                qs.setStatus(QuestStatus.REWARD);
                                updateQuestStatus(player, qs);
                                QuestService.questTimerEnd(env);
                                return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 5);
                            } else
                                return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 2716);
                        }
                }
            }
        }
        return defaultQuestEndDialog(env);
    }
}
